import store from '@/store'

/**
 * @param {Array} value
 * @returns {Boolean}
 * @example see @/views/permission/directive.vue
 */
export function checkPermission(value) {
  console.log('store.getters.permissions',store.getters.permissions.map(item => item.permissionName))
  if (value && value instanceof Array && value.length > 0) {
    const permissions = store.getters && store.getters.permissions
    const requiredPermissions = value

    const hasPermission = permissions.some(item => {
      return requiredPermissions.includes(item.permissionName)
    })
    return hasPermission
  } else {
    console.error(`need roles! Like v-permission="['admin','editor']"`)
    return false
  }
}

/**
 * @param {Array} value
 * @returns {Boolean}
 */
export function checkUserPermission(value) {
  if (value) {
    const user_id = store.getters && store.getters.id

    return value === user_id
  } else {
    console.error(`id validation`)
    return false
  }
}

/**
 * @param {Number} projectId
 * @returns {Boolean}
 */
export function checkProjectOwner(projectId, projectApplicantId) {
  if (projectId || projectApplicantId) {
    let isOwner = false;
    if (store.getters && store.getters.projects && store.getters.projects.length > 0) {
      for(const project of store.getters.projects) {
        if (project.projectId === projectId) {
          isOwner = true;
          break;
        }
      }
    }
    if (projectApplicantId === store.getters.id) {
      isOwner = true
    }
      return isOwner;
  } else {
    console.error(`id validation`)
    return false
  }
}

/**
 * @param {string} date
 * @returns {Boolean}
 */
export function checkDate(date) {
  return new Date(date).getTime() > new Date().getTime();
}
